package com.gullmall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gullmall.common.utils.PageUtils;
import com.gullmall.common.utils.Query;

import com.gullmall.product.dao.SkuInfoDao;
import com.gullmall.product.entity.SkuInfoEntity;
import com.gullmall.product.service.SkuInfoService;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 列表
     * {t=1655275279380, page=1, limit=10, key=, catelogId=0,
     * brandId=0, min=0, max=0} min/max为价格.id或价格0则为查全部
     */
    @Override
    public PageUtils querySkuInfoPage(Map<String, Object> params) {

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.like("sku_name", key);
        }

        String catalogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catalogId) && !"0".equals(catalogId)) {
            wrapper.eq("catalog_id", catalogId);
        }

        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !"0".equals(brandId)) {
            wrapper.eq("brand_id", brandId);
        }

        int min = Integer.parseInt((String) params.get("min"));

        if (!StringUtils.isEmpty(min) && min != 0) {
            //.ge大于等于
            wrapper.ge("price", min);
        }
        int max = Integer.parseInt((String) params.get("max"));
        if (!StringUtils.isEmpty(max) && max != 0 && max >= min) {
            //.le小于等于
            wrapper.le("price", max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    //根据spuId查询sku
    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {
        return  this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
    }

}